package fashionshop.gui_old;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.sql.ResultSet;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;

import fashionshop.bll.CategoryBLL;
import fashionshop.bll.ProductBLL;
import fashionshop.utility.ImageUtility;

public class ProductUI {
	
	private String PATH = "D:\\img\\";
	JButton btnUpload;
	private JTable productTable;
	private Vector productTableTitle;
	private Vector productTableRecords;
    
	private JLabel labelId;
	private JLabel labelCategoryName;
	private JLabel labelName;
	private JLabel labelCode;
	private JLabel labelQuantity;
	private JLabel labelImage;
	private JLabel labelColor;
	private JLabel labelPrice;
	private JLabel labelDescription;
	private JLabel labelCreateDate;
	private JLabel labelSalePrice;
	
	private JTextField txtId;
	private JComboBox cbCategoryName;
	private JTextField txtName;
	private JTextField txtCode;
	private JTextField txtQuantity;
	private JLabel labelShowImage;
	private JTextField txtColor;
	private JTextField txtPrice;
	private JTextField txtDescription;
	private JTextField txtCreateDate;
	private JTextField txtSalePrice;
	
	public ProductUI() throws Exception{
		productTable = new JTable();
		productTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		productTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "none");
		productTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "none");
		productTableTitle = new Vector();
		productTableRecords = new Vector();
		productTableTitle.add("ID");
		productTableTitle.add("Phân loại");
		productTableTitle.add("Tên sản phẩm");
		productTableTitle.add("Mã sản phẩm");
		productTableTitle.add("Số lượng");
		productTableTitle.add("Hình ảnh");
		productTableTitle.add("Màu sắc");
		productTableTitle.add("Giá ti�?n");
		productTableTitle.add("Mô tả");
		productTableTitle.add("Ngày tạo");
		productTableTitle.add("Giá khuyến mãi");
		ProductBLL productBll = new ProductBLL();
		productTableRecords = productBll.loadAllProduct();
		productTable.setModel(new DefaultTableModel(productTableRecords, productTableTitle));
		
		labelId= new JLabel("ID");
		labelCategoryName = new JLabel("Phân loại");
		labelName = new JLabel("Tên sản phẩm");
		labelCode = new JLabel("Mã sản phẩm");
		labelQuantity = new JLabel("Số lượng");
		labelImage = new JLabel("Hình ảnh");
		labelColor = new JLabel("Màu sắc");
		labelPrice = new JLabel("Giá ti�?n");
		labelDescription = new JLabel("Mô tả");
		labelCreateDate = new JLabel("Ngày tạo");
		labelSalePrice = new JLabel("Giá khuyến mãi");
		
		txtId = new JTextField();
		txtId.setEnabled(false);
		cbCategoryName = new JComboBox();
		ResultSet result = CategoryBLL.loadAllCategory();
		while (result.next()){
			cbCategoryName.addItem(result.getString("Name"));
		}
		txtName = new JTextField();
		txtCode = new JTextField();
		txtQuantity = new JTextField();
		labelShowImage = new JLabel();
		txtColor = new JTextField();
		txtPrice = new JTextField();
		txtDescription = new JTextField();
		txtCreateDate = new JTextField();
		txtSalePrice = new JTextField();
		
		
		
		btnUpload = new JButton();
		btnUpload.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				 JFileChooser chooser = new JFileChooser();
				 chooser.setMultiSelectionEnabled(false);
				 FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
				 chooser.setFileFilter(filter);
			     int option = chooser.showOpenDialog(MainScreen.detailPanel);
			     if (option == JFileChooser.APPROVE_OPTION) {
			     txtCode.setText(chooser.getSelectedFile().getName());
			     }
			     else {
//			    	 txtCode.setText();
			     }
			}
		});
		productTable.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				int selectedRow = productTable.getSelectedRow();
	            if (selectedRow != -1) {
	                Vector currentProduct = (Vector) productTableRecords.get(selectedRow);
	                txtId.setText(currentProduct.get(0).toString());
					cbCategoryName.setSelectedItem(currentProduct.get(1).toString());
	        		txtName.setText(currentProduct.get(2).toString());
	        		txtCode.setText(currentProduct.get(3).toString());
	        		txtQuantity.setText(currentProduct.get(4).toString());
	        		txtColor.setText(currentProduct.get(6).toString());
	        		txtPrice.setText(currentProduct.get(7).toString());
	        		txtDescription.setText(currentProduct.get(8).toString());
	        		txtCreateDate.setText(currentProduct.get(9).toString());
	        		txtSalePrice.setText(currentProduct.get(10).toString());
	        		
//					BufferedImage img = null;//new BufferedImage(50, 50, 0);
//					try {
//						img = ImageIO.read(new File(PATH + currentProduct.get(5).toString()));
//					} catch (IOException e1) {
//						// TODO Auto-generated catch block
//						e1.printStackTrace();
//					}
//					img = ImageUtility.scaleImage(img, 100, 100, null);
//	                ImageIcon icon=new ImageIcon(img);
//	                labelShowImage.setIcon(icon);
//	        		File f = new File(PATH + currentProduct.get(5).toString());
	        		if(new File(PATH + currentProduct.get(5).toString()).isFile()){
	        			BufferedImage img = null;//new BufferedImage(50, 50, 0);
						try {
							img = ImageIO.read(new File(PATH + currentProduct.get(5).toString()));
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						img = ImageUtility.scaleImage(img, 100, 100, null);
		                ImageIcon icon=new ImageIcon(img);
		                labelShowImage.setIcon(icon);
	        		}
	        		else{
	        			labelShowImage.setIcon(null);
	        		}
	        		
	            }
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
			}
		});
		
	}
	
	public void loadAllProduct(){
		MainScreen.tablePanel.removeAll();
		MainScreen.tablePanel.setLayout(new BorderLayout());
		MainScreen.tablePanel.add(new JScrollPane(productTable), BorderLayout.CENTER);
		MainScreen.tablePanel.revalidate();
	}
	
	public void loadProductDetail(){

		MainScreen.detailPanel.setLayout(new GridLayout(1, 1));
		MainScreen.detailPanel.removeAll();
		JPanel formEdit = new JPanel();
		formEdit.setLayout(new GridLayout(0, 12));
		formEdit.removeAll();
	    formEdit.add(labelId);
	    formEdit.add(txtId);
	    formEdit.add(labelCategoryName);
	    formEdit.add(cbCategoryName);
	    formEdit.add(labelName);
	    formEdit.add(txtName);
	    formEdit.add(labelCode);
	    formEdit.add(txtCode);
	    formEdit.add(labelQuantity);
	    formEdit.add(txtQuantity);
	    formEdit.add(labelColor);
	    formEdit.add(txtColor);
	    formEdit.add(labelPrice);
	    formEdit.add(txtPrice);
	    formEdit.add(labelDescription);
	    formEdit.add(txtDescription);
	    formEdit.add(labelCreateDate);
	    formEdit.add(txtCreateDate);
	    formEdit.add(labelDescription);
	    formEdit.add(txtDescription);
	    formEdit.add(labelSalePrice);
	    formEdit.add(txtSalePrice);
	    formEdit.add(labelImage);
	    formEdit.add(labelShowImage);
	    formEdit.add(btnUpload);
        MainScreen.detailPanel.add(formEdit);
        MainScreen.detailPanel.setVisible(true);
	}
	
}
